package io.qifan.desktop.client.event;

import io.qifan.desktop.client.store.token.TokenStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class ApplicationEventListener {
    @Autowired
    TokenStore tokenStore;
    @Autowired
    ApplicationEventPublisher eventPublisher;

    @EventListener
    public void handleApplicationStarted(ApplicationStartedEvent applicationStartedEvent) {
        Optional.ofNullable(tokenStore.getToken()).ifPresent(token -> {
            log.info("存在token：{}", token);
            eventPublisher.publishEvent(new Events.LoginSuccessEvent(token));
        });
    }
}
